/**************************************************************************************
 *  Copyright (c)  2010  Leszek Karcz.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package main;
import java.awt.Point;
import java.awt.event.MouseEvent;

import view.*;
import data.*;

/**************************************************************************************
 * Author(s): Leszek Karcz
 * Interpreter class receives user input and translates it into method calls to other
 * classes
 **************************************************************************************/
public class Interpreter {
	private int mode; //0 - StartMenu; 1 - Gameplay; 2 - Overview
	private View view;
	private Gameplay gameplay;
	private GameplayPlayerData gameplayPlayerData;
	private OverviewData overviewData;
	private StartScreenData startScreenData;
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Interpreter constructor
	 **************************************************************************************/
	public Interpreter()
	{
		this.mode=0;
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * adds View
	 **************************************************************************************/
	public void addView(View view)
	{
		this.view=view;		
	}	

	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * adds StartScreenData
	 **************************************************************************************/
	public void addStartScreen(StartScreenData startScreenData)
	{
		this.startScreenData=startScreenData;
	}	
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * adds GameplayPlayerData
	 **************************************************************************************/
	public void addGameplay(Gameplay gameplay, GameplayPlayerData gameplayPlayerData)
	{
		this.gameplay=gameplay;
		this.gameplayPlayerData=gameplayPlayerData;		
	}	
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * adds OverviewData
	 **************************************************************************************/
	public void addOverview(OverviewData overviewData)
	{
		this.overviewData=overviewData;
	}	
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * receives input and depending on the mode passes it to specialized interpreter method
	 **************************************************************************************/
	public void interpret(String key)
	{
		if(this.mode==0)
		{
			startScreenInterpret(key);
		}
		else if(this.mode==1)
		{
			gameplayInterpret(key);
		}	
		else if(this.mode==2)
		{
			overviewInterpret(key);
		}	
	}	
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * interpreter method for start screen
	 **************************************************************************************/
	public void startScreenInterpret(String key)
	{
		if((this.startScreenData!=null))
		{
			if(key.equals("commandNext"))
			{
				this.startScreenData.next();
				this.view.refresh(0);
			}
			else if(key.equals("commandPrev"))
			{
				this.startScreenData.previous();
				this.view.refresh(0);
			}
			else if(key.equals("Issue"))
			{
				if(this.startScreenData.execute())
				{
					this.mode=1;
					this.view.setMode(this.mode);
					this.gameplay.startGameplay();
				}
			}
		
		}
	}	
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * interpreter method for start menu
	 **************************************************************************************/
	public void gameplayInterpret(String key)
	{
		if((this.gameplayPlayerData!=null) && (this.view!=null))
		{
			if(this.gameplayPlayerData.isClockRunning())
				{
				if(key.equals("SW"))
				{
					this.gameplayPlayerData.moveCursorPosition(4);
					this.view.refresh(1);
				}
				else if(key.equals("S"))
				{
					this.gameplayPlayerData.moveCursorPosition(3);
					this.view.refresh(1);
				}
				else if(key.equals("SE"))
				{
					this.gameplayPlayerData.moveCursorPosition(2);
					this.view.refresh(1);
				}
				else if(key.equals("Focus"))
				{
					this.gameplayPlayerData.moveCursorOnSelected();
					this.view.refresh(1);
				}
				else if(key.equals("NW"))
				{
					this.gameplayPlayerData.moveCursorPosition(5);
					this.view.refresh(1);
				}
				else if(key.equals("N"))
				{
					this.gameplayPlayerData.moveCursorPosition(0);
					this.view.refresh(1);
				}
				else if(key.equals("NE"))
				{
					this.gameplayPlayerData.moveCursorPosition(1);
					this.view.refresh(1);
				}
				else if(key.equals("commandNext"))
				{
					this.gameplayPlayerData.changeSelection(3, false);
					this.view.refresh(1);
				}
				else if(key.equals("commandPrev"))
				{
					this.gameplayPlayerData.changeSelection(3, true);
					this.view.refresh(1);
				}
				else if(key.equals("modeNext")){
					gameplayPlayerData.selectionCycling(0, 1);
					this.view.refresh(1);
				}
				else if(key.equals("modePrev")){
					gameplayPlayerData.selectionCycling(0, 0);
					this.view.refresh(1);
				}
				else if(key.equals("typeNext")){
					gameplayPlayerData.selectionCycling(1, 1);
					this.view.refresh(1);
				}
				else if(key.equals("typePrev")){
					gameplayPlayerData.selectionCycling(1, 0);
					this.view.refresh(1);
				}
				else if(key.equals("instanceNext")){
					gameplayPlayerData.selectionCycling(2, 1);
					this.view.refresh(1);
				}
				else if(key.equals("instancePrev")){
					gameplayPlayerData.selectionCycling(2, 0);
					this.view.refresh(1);
				}
				else if(key.equals("Pause"))
				{
					this.gameplay.stopGameplay();
					this.view.refresh(1);
				}
				else if(key.equals("editControls"))
				{
					EditControlView ecv = new EditControlView(view.getKeyDefs());
					
					this.view.refresh(1);
				}
				else if(key.equals("Tab"))
				{
					this.gameplayPlayerData.toggleDisplayResourceMode();
					this.view.refresh(1);
				}
				else if(key.equals("Issue"))
				{
					this.gameplayPlayerData.executeSelected("Issue");
					this.view.refresh(1);
				}
				else if(key.equals("#0"))
				{
					this.gameplayPlayerData.executeSelected("0");
					this.view.refresh(1);
				}
				else if(key.equals("#1"))
				{
					this.gameplayPlayerData.executeSelected("1");
					this.view.refresh(1);
				}
				else if(key.equals("#2"))
				{
					this.gameplayPlayerData.executeSelected("2");
					this.view.refresh(1);
				}
				else if(key.equals("#3"))
				{
					this.gameplayPlayerData.executeSelected("3");
					this.view.refresh(1);
				}
				else if(key.equals("#4"))
				{
					this.gameplayPlayerData.executeSelected("4");
					this.view.refresh(1);
				}
				else if(key.equals("#5"))
				{
					this.gameplayPlayerData.executeSelected("5");
					this.view.refresh(1);
				}
				else if(key.equals("#6"))
				{
					this.gameplayPlayerData.executeSelected("6");
					this.view.refresh(1);
				}
				else if(key.equals("#7"))
				{
					this.gameplayPlayerData.executeSelected("7");
					this.view.refresh(1);
				}
				else if(key.equals("#8"))
				{
					this.gameplayPlayerData.executeSelected("8");
					this.view.refresh(1);
				}
				else if(key.equals("#9"))
				{
					this.gameplayPlayerData.executeSelected("9");
					this.view.refresh(1);
				}
				else if(key.equals("createRally"))
				{
					this.gameplayPlayerData.createRally();
					this.view.refresh(1);
				}
				else if(key.equals("Cancel"))
				{
					this.gameplayPlayerData.selectedResetCommandMode();
					this.view.refresh(1);
				}
				else if(key.equals("switchView"))
				{
					this.gameplay.stopGameplay();
					this.mode=2;
					this.view.setMode(2);					
					this.view.refresh(2);
				}
				else if(key.equals("switchToTree")){
					System.out.println("Added code to switch to Tree");
				}
			}
			else
			{
				if(key.equals("Pause"))
				{
					this.gameplay.startGameplay();
					this.view.refresh(1);
				}
				else if(key.equals("switchView"))
				{
					this.mode=2;
					this.view.setMode(2);	
					this.view.refresh(2);
				}
			}
		}
	}	
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * interpreter method for overview
	 **************************************************************************************/
	public void overviewInterpret(String key)
	{
		if((this.overviewData!=null) && (this.view!=null))
		{
			if(key.equals("instanceNext")){
				this.overviewData.moveCursorRight();
				this.view.refresh(2);
			}
			else if(key.equals("instancePrev")){
				this.overviewData.moveCursorLeft();
				this.view.refresh(2);
			}
			else if(key.equals("commandNext"))
			{
				this.overviewData.moveCursorUp();
				this.view.refresh(2);
			}
			else if(key.equals("commandPrev"))
			{
				this.overviewData.moveCursorDown();
				this.view.refresh(2);
			}
			else if(key.equals("typeNext")){
				this.overviewData.nextMode();
				this.view.nextOverviewMode();
				this.view.refresh(2);
			}
			else if(key.equals("typePrev")){
				this.overviewData.previousMode();
				this.view.previousOverviewMode();
				this.view.refresh(2);
			}
			else if(key.equals("switchView"))
			{
				this.mode=1;
				this.view.setMode(1);
				this.gameplay.startGameplay();
			}
		}
	}	
	
	/**************************************************************************************
	 * Author(s): Kyle Van Wie
	 * interpreter mouse clicks
	 **************************************************************************************/
	public void mouseClickInterpret(MouseEvent evt){
		
		int pntX = evt.getX();
		int pntY = evt.getY();
		int centX = 442;
		int centY = 345;
		//1 left, 3 right
		int button = evt.getButton();
		//System.out.println("*****Click*****");
		//System.out.println("pntX: " + pntX + ", pntY:" + pntY);
		//System.out.println("disX: " + (centX - pntX) + ", disY:" + (centY - pntY));
		
		//Mouse clicks for gameplay
		if((this.gameplayPlayerData!=null) && (this.view!=null) && mode == 1){
			if(this.gameplayPlayerData.isClockRunning()){
				//Go to Selected Controllable
				//X: 833 to 1011
				//Y: 51 to 92
				if(pntX >= 833 && pntX <= 1011 && pntY >= 51 && pntY <= 92){
					this.gameplayPlayerData.moveCursorOnSelected();
					this.view.refresh(1);
				}
				//Command Cycling
				//X: 839 to 1012
				//Y: 293 to 517
				if(pntX >= 839 && pntX <= 1012 && pntY >= 293 && pntY <= 517){
					if(button == 1){
						this.gameplayPlayerData.changeSelection(3, false);
					}
					if(button == 3){
						this.gameplayPlayerData.changeSelection(3, true);
					}
					this.view.refresh(1);
				}
				//Pause
				//X: 161 to 262
				//Y: 28  to 47
				if(pntX >= 161 && pntX <= 262 && pntY >= 28 && pntY <= 47){
					this.gameplay.stopGameplay();
					this.view.refresh(1);
				}
				//Move Cursor
				//Area View
					//X: 8  to 821
					//Y: 55 to 579
				if(pntX >= 8 && pntX <= 821 && pntY >= 55 && pntY <= 579){
					//Middle
					//X: 407 to 474
					//Y: 288 to 403
					if(pntX >= 407 && pntX <= 474 && pntY >= 288 && pntY <= 403){
						//System.out.println("Cursor Clicked!");
					}
					//North of Middle
					//X: 407 to 474
					//Y: 55 to 289
					else if(pntX >= 407 && pntX <= 474 && pntY >= 55 && pntY <= 289){
						//System.out.println("North");
						this.gameplayPlayerData.moveCursorPosition(0);
						this.view.refresh(1);
					}
					//NorthEast of Middle
					//X: 474 to 821
					//Y: 55 to 345
					else if(pntX >= 474 && pntX <= 821 && pntY >= 55 && pntY <= 345){
						//System.out.println("NorthEast");
						this.gameplayPlayerData.moveCursorPosition(1);
						this.view.refresh(1);
					}
					//NorthWest of Middle
					//X: 8 to 406
					//Y: 55 to 345
					else if(pntX >= 8 && pntX <= 406 && pntY >= 55 && pntY <= 345){
						//System.out.println("NorthWest");
						this.gameplayPlayerData.moveCursorPosition(5);
						this.view.refresh(1);
					}
					//South of Middle
					//X: 407 to 474
					//Y: 404 to 579
					else if(pntX >= 407 && pntX <= 474 && pntY >= 404 && pntY <= 579){
						//System.out.println("South");
						this.gameplayPlayerData.moveCursorPosition(3);
						this.view.refresh(1);
					}
					//SouthEast of Middle
					//X: 475 to 821
					//Y: 345 to 579
					else if(pntX >= 475 && pntX <= 821 && pntY >= 345 && pntY <= 579){
						//System.out.println("SouthEast");
						this.gameplayPlayerData.moveCursorPosition(2);
						this.view.refresh(1);
					}
					//SouthWest of Middle
					//X: 8   to 406
					//Y: 345 to 579
					else if(pntX >= 8 && pntX <= 406 && pntY >= 345 && pntY <= 579){
						//System.out.println("SouthWest");
						this.gameplayPlayerData.moveCursorPosition(4);
						this.view.refresh(1);
					}
				}
			}
			else{
				//Unpause
				//X: 161 to 262
				//Y: 28  to 47
				if(pntX >= 161 && pntX <= 262 && pntY >= 28 && pntY <= 47){
					this.gameplay.startGameplay();
					this.view.refresh(1);
				}
			}
		}
		//Mouse Clicks for Start Screen
		else if((this.startScreenData!=null)&& (this.view!=null) && mode == 0){
			//"Land"
			//X: 440 to 585
			//Y: 435 to 460
			if(pntX >= 440 && pntX <= 585 && pntY >= 435 && pntY <= 460){
				this.mode=1;
				this.view.setMode(this.mode);
				this.gameplay.startGameplay();
			}
			//"Abort"
			//X: 440 to 585
			//Y: 461 to 486
			if(pntX >= 440 && pntX <= 585 && pntY >= 461 && pntY <= 486){
				System.exit(0);
			}
		}
	}
}
